using System;
using Actions;
using Cinemachine;
using UnityEngine;

namespace Visuals
{
    public class ScreenShaker : MonoBehaviour
    {
        [SerializeField] private Shooter shooter;
        
        private CinemachineImpulseSource _source;


        private void Awake()
        {
            _source = GetComponent<CinemachineImpulseSource>();
        }

        private void OnEnable()
        {
            shooter.AnyTargetDied += Shooter_AnyTargetDied;
            PlayerChaser.AnyTargetHitPlayer += PlayerChaser_AnyTargetHitPlayer;
        }

        private void OnDisable()
        {
            shooter.AnyTargetDied -= Shooter_AnyTargetDied;
            PlayerChaser.AnyTargetHitPlayer -= PlayerChaser_AnyTargetHitPlayer;
        }
        
        private void PlayerChaser_AnyTargetHitPlayer()
        {
            Shake();
        }

        private void Shooter_AnyTargetDied()
        {
            Shake();
        }

        public void Shake(float intensity = 1f)
        {
            _source.GenerateImpulse(intensity);
        }
    }
}